Skip to content

Add quick flag and use unsigned unlifted Word##198

Open
kquick wants to merge 5 commits intomasterfrom
quick_flag
Open

Add quick flag and use unsigned unlifted Word##198
kquick wants to merge 5 commits intomasterfrom
quick_flag

Conversation

@kquick
Copy link
Member

@kquick kquick commented Sep 13, 2022

This makes further performance modifications to use unsigned Word# instead of signed Int# and adds a quick flag (disabled by default). When the quick flag is enabled, much of the error detection, verification, and other internal checks are removed; this mode is a further incremental performance gain but it should only be used in situations where well-formed bitcode from known-supported versions of LLVM willl be used.

@kquick kquick requested a review from RyanGlScott September 13, 2022 00:41
where
byte = toBitString (Bits' 8)
bcWrapperMagicConst :: Word
bcWrapperMagicConst = 0x0b16c0de
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wait, did this code change? The previous version used 17, whereas this version uses 16.

Comment on lines +94 to +100
-- Verify a Word is 64-bits (at compile time)
$(return $ if isTrue# ((int2Word# 3#) `eqWord#`
(((int2Word# 0xF0#) `uncheckedShiftL#` 58#)
`uncheckedShiftRL#` 62#))
then []
else error "Word type must be 64-bits!"
)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm very leery about making this library completely unbuildable on 32-bit architectures. Why not just use Word64 instead?

{-# LANGUAGE CPP #-}
{-# LANGUAGE MagicHash #-}
{-# LANGUAGE PatternSynonyms #-}
{-# LANGUAGE QuasiQuotes #-}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As far as I can tell, the QuasiQuotes extension is never used anywhere in this module, just TemplateHaskell.

Comment on lines +141 to +143
#ifdef QUICK
fromBitString (BitString _ i) = x
#else
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not a fan of sprinkling CPP everywhere throughout the library like this, since (1) it makes adds a lot of lexical noise, and (2) it makes it more difficult to remember to keep the two versions of every function in sync, since you have to remember to build with both -fquick and -f-quick unless you want to risk bitrot.

Rather this going with this approach, why not do something like this instead?

fromBitString :: (Num a, Bits a) => BitString -> a
fromBitString (BitString l i)
  | quickMode = x
  | otherwise =
    case bitSizeMaybe x of
      ...

Where:

quickMode :: Bool
#if defined(QUICK)
quickMode = True
#else
quickMode = False
#endif

This way, we can continue to typecheck both versions of each function, decreasing the chances of bitrot. Since quickMode will statically be known to be True or False, the optimizer will be able to make swift work of it.

-> ByteString {-^ the ByteString to extract from -}
-> Either String (() -> (# Int#, Int# #))
-> Either String (() -> (# Word#, Int# #))
extractFromByteString !bitLim# !sBit# !nbits# bs =
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now that we have a quick flag, is it worth making extractFromByteString' above the quick version of this function?

Comment on lines +428 to +430
#ifndef QUICK
Assert.recordSizeGreater r (1 + asmStrSize)
#endif
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similarly, rather than surrounding every assertion with CPP, it would be better to redefine each assertion function in terms of quickMode. Something like:

recordSizeGreater x y = Control.Monad.unless quickMode $ ...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants